package com.example.wxback.push;

import com.alibaba.fastjson.JSONObject;
import com.example.wxback.model.PushInfo;
import com.example.wxback.model.PushResultInfo;

import java.util.ResourceBundle;

public class PushUtil {
    private static String appAndroidkey = "614e934966b59330aa70045d";
    private static String appAndroidMasterSecret = "zy7rr2iq9hlgneq8mna5kvmdhvgvcykm";

    private static String appIOSkey = "5f9fa9fe45b2b751a9215833";
    private static String appIOSMasterSecret = "z0ekqxdrqle2gby2vkg8ukwuxp1lvmng";

    private static String aliasType = "push_invitecode";    //别名类型，与手机端统一的一个自定义类型 值可随便写但必须统一

    // http接口：http://msg.umeng.com/api/send
    // https接口：https://msgapi.umeng.com/api/send
    private static String url = "http://msg.umeng.com/api/send";
    private static boolean mode = false;    //模式 true正式模式；false测试模式
    private static PushClient client = new PushClient();

    public static final int PUSH_TYPE_ALIAS = 1;//按别名推送
    public static final int PUSH_TYPE_ALL = 2;//推送给所有人

    static {
        if (appAndroidkey.equals("")) {
            ResourceBundle resource = ResourceBundle.getBundle("push");
            appAndroidkey = resource.getString("appAndroidkey");
            appAndroidMasterSecret = resource.getString("appAndroidMasterSecret");
            appAndroidkey = resource.getString("appIOSkey");
            appAndroidMasterSecret = resource.getString("appIOSMasterSecret");
            aliasType = resource.getString("aliasType");
            url = resource.getString("url");
            mode = resource.getObject("mode") == "true" ? true : false;
        }
    }

    private static JSONObject getAndroidParamData(String phone) {
        JSONObject paramMap = new JSONObject();
        paramMap.put("appkey", appAndroidkey);
        String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
        paramMap.put("timestamp", timestamp);

        paramMap.put("type", "broadcast");
        paramMap.put("alias", phone);
        paramMap.put("alias_type", "alias-phone");

        JSONObject payload = new JSONObject();

        JSONObject body = new JSONObject();
        body.put("title", "");
        body.put("text", "");
        body.put("ticker", "");
        body.put("after_open", "go_custom");
        body.put("custom", phone);

        payload.put("body", body);
//        payload.put("extra", JSONObject.toJSON(jsonObject));
        paramMap.put("payload", payload);
        paramMap.put("production_mode", mode);
        payload.put("display_type", "message");

        return paramMap;
    }

    private static JSONObject getIOSParamData(int pushType, PushInfo pushInfo) {
        JSONObject paramMap = new JSONObject();
        paramMap.put("appkey", appIOSkey);
        String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
        paramMap.put("timestamp", timestamp);

        if (PUSH_TYPE_ALIAS == pushType) {
            paramMap.put("type", "customizedcast");
            paramMap.put("alias", pushInfo.getAlias());
            paramMap.put("alias_type", aliasType);
        } else {
            paramMap.put("type", "broadcast");
        }

        JSONObject payload = new JSONObject();
        payload.put("display_type", "notification");

        JSONObject aps = new JSONObject();
        JSONObject alert = new JSONObject();
        alert.put("title", pushInfo.getTitle());
        alert.put("subtitle", pushInfo.getSubTitle());
        payload.put("path", pushInfo.getRouter());

        aps.put("alert", alert);
        aps.put("sound", "default");

        payload.put("aps", aps);


        paramMap.put("payload", payload);
        paramMap.put("production_mode", mode);

        return paramMap;
    }

    /**
     * 发送安卓别名推送通知
     *
     * @throws Exception
     */
    public static PushResultInfo sendAndroidCustomizedcast(String phone) throws Exception {
        JSONObject paramMap = getAndroidParamData(phone);
        if (paramMap == null) {
            PushResultInfo pushResultInfo = new PushResultInfo();
            pushResultInfo.setSuccess(false);
            pushResultInfo.setContent("构建推送消息失败");
            return pushResultInfo;
        }
        return client.send(url, paramMap.toJSONString(), appAndroidMasterSecret);
    }

    /**
     * 发送苹果别名推送通知
     *
     * @throws Exception
     */
    public static PushResultInfo sendIOSCustomizedcast(int pushType, PushInfo pushInfo) throws Exception {
        JSONObject paramMap = getIOSParamData(pushType, pushInfo);
        if (paramMap == null) {
            PushResultInfo pushResultInfo = new PushResultInfo();
            pushResultInfo.setSuccess(false);
            pushResultInfo.setContent("构建推送消息失败");
            return pushResultInfo;
        }
        return client.send(url, paramMap.toJSONString(), appIOSMasterSecret);
    }

    public static void main(String[] args) {
        try {
            PushInfo pushInfo = new PushInfo();
            pushInfo.setAlias("18888888881");
            pushInfo.setTitle("这是消息标题");
            pushInfo.setSubTitle("这是副标题");

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", "1");
            jsonObject.put("path", "/personal/invite_code");

            pushInfo.setCustom(jsonObject);

//            sendIOSCustomizedcast(PushUtil.PUSH_TYPE_ALIAS, pushInfo);
            sendAndroidCustomizedcast("18888888881");
        } catch (Exception ex) {
            ex.printStackTrace();
            //如果在本地要测试,则去友盟后台取消白名单,然后mode设置为true(正式环境).即可发送成功
            /**
             * 本地的json数据,
             */
            //{"alias":"17671632486","alias_type":"CoinLa.PhoneNumber","appkey":"5a9fa4dbf43e48028e000237",
            //"description":"测试消息111","payload":{"aps":{"alert":{"body":"测试消息1111","title":"测试消息"},"badge":1,"sound":"default"},
            //"display_type":"notification"},"production_mode":true,"timestamp":"1525855267","type":"customizedcast"}
            /**
             * 线上测试的json数据
             */
            //{"policy":{"expire_time":"2018-05-12 16:32:52"},"description":"阿达",
            //"production_mode":true,"appkey":"5a9fa4dbf43e48028e000237",
            //"payload":{"aps":{"alert":{"title":"阿达","subtitle":"阿达","body":"阿达"},"sound":"default"}},
            //"alias":"17671632486","alias_type":"CoinLa.PhoneNumber","type":"customizedcast","timestamp":"1525854888505"}
        }
    }
}